﻿using System;
using System.Linq;
using System.Text;

namespace DBMock.Builders
{
    /// <summary>
    /// 中文姓名builder
    /// </summary>
    public class ChineseNameBuilder : Builder
    {
        //姓
        private const string names =
            "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞" +
            "仁袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏" +
            "成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘" +
            "缪干解鹰宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷" +
            "车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟博印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥" +
            "能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖" +
            "庾终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后荆红游";

        //十六进制字符
        private char[] chars;

        /// <summary>
        /// 初始化ChineseBuilder
        /// </summary>
        /// <param name="seed">随机数种子</param>
        public ChineseNameBuilder(int seed)
            : base(seed)
        {
            chars = "0123456789abcdef".ToCharArray();
        }

        public override object Build()
        {
            int length = random.Next(1, 3); //【名】长度
            byte[][] bytes = new byte[length][]; //【名】字节数组

            var gb = Encoding.GetEncoding("gb2312"); //字符编码

            for (int i = 0; i < length; i++)
            {
                var low = new char[2]; //低位字节字符
                var high = new char[2]; //高位字节字符

                low[0] = chars[random.Next(11, 14)];

                low[1] = low[0] == 'd' ? chars[random.Next(0, 7)] :
                                         chars[random.Next(0, 16)];

                high[0] = chars[random.Next(10, 16)];

                high[1] = high[0] == 'a' ? chars[random.Next(1, 16)] :
                          high[0] == 'f' ? chars[random.Next(0, 15)] :
                                           chars[random.Next(0, 16)];

                byte lowByte = Convert.ToByte(new string(low), 16); //低位字节
                byte highByte = Convert.ToByte(new string(high), 16); //高位字节

                bytes[i] = new byte[] { lowByte, highByte }; //一个中文字符字节数组
            }

            char familyName = names[random.Next(names.Length)];
            string foreName = bytes.Select(b => gb.GetString(b)).Aggregate((a, b) => a + b);

            return familyName + foreName;
        }
    }
}
